﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lottery.Control {
    /// <summary>
    /// 大中小数差
    /// </summary>
    public partial class SCControl : UserControl {
        public SCControl() {
            InitializeComponent();

            editPreNumber.Items.AddRange(new string[] { "", "3", "1", "0", "310" });
            editPreNumber.SelectedIndex = 0;
        }

        /// <summary>
        /// 大差
        /// </summary>
        public string D { get; set; }

        /// <summary>
        /// 中差
        /// </summary>
        public string Z { get; set; }

        /// <summary>
        /// 小差
        /// </summary>
        public string X { get; set; }


        public int Three { get { return edit3.Value; } set { edit3.Text = value.ToString(); } }

        public int One { get { return edit1.Value; } set { edit1.Text = value.ToString(); } }

        public int Zero { get { return edit0.Value; } set { edit0.Text = value.ToString(); } }


        /// <summary>
        /// 预测奖号
        /// </summary>
        public string PreNumber {
            get { return editPreNumber.Text; }
            set { editPreNumber.SelectedIndex = (value == null) ? 0 : editPreNumber.Items.IndexOf(value); }
        }

        private System.Windows.Forms.Control jumpToNext;
        /// <summary>
        /// 按回车键的时候，焦点跳转到的控件
        /// </summary>
        public System.Windows.Forms.Control JumpToNext {
            get { return jumpToNext; }
            set {
                jumpToNext = value;
                edit0.JumpToNext = value;
            }
        }

        /// <summary>
        /// 数据是否有效。
        /// </summary>
        public bool IsOK { get; private set; }

        private int index;

        /// <summary>
        /// 场次
        /// </summary>
        public int Order {
            get { return index; }
            set {
                index = value;
                labelOrder.Text = string.Format("第 {0} 场", index);
            }
        }

        public void CalcDZX() {
            if (edit0.Text == string.Empty || edit1.Text == string.Empty || edit3.Text == string.Empty) {
                IsOK = false;
                D = Z = X = string.Empty;
                return;
            }

            IsOK = true;
            // 310的媒体统计数据相同的时候，本次就无效
            if (edit0.Text == edit1.Text && edit0.Text == edit3.Text) {
                editD.Text = editZ.Text = editX.Text = string.Empty;
                D = Z = X = string.Empty;
                return;
            }

            var data = new int[] { edit3.Value, edit1.Value, edit0.Value };
            var list = (from item in data
                        orderby item descending
                        select item).ToArray();
            if (list[1] == list[2]) {
                D = X = string.Empty;
                Z = (list[0] - list[1]).ToString();
                editD.Text = editX.Text = string.Empty;
                editZ.Text = Z;
                return;
            } else if (list[0] == list[1]) {
                D = X = string.Empty;
                Z = (list[1] - list[2]).ToString();
                editD.Text = editX.Text = string.Empty;
                editZ.Text = Z;
                return;
            }

            var d = list[0] - list[2];
            var z = list[0] - list[1];
            var x = list[1] - list[2];
            D = d.ToString();
            Z = z.ToString();
            X = x.ToString();
            editD.Text = d.ToString();
            editZ.Text = z.ToString();
            editX.Text = x.ToString();
        }

        public void Set310(int? three, int? one, int? zero) {
            edit3.Text = three.HasValue ? three.ToString() : string.Empty;
            edit1.Text = one.HasValue ? one.ToString() : string.Empty;
            edit0.Text = zero.HasValue ? zero.ToString() : string.Empty;
        }

        public void Clear() {
            edit3.Text = string.Empty;
            edit1.Text = string.Empty;
            edit0.Text = string.Empty;

            editD.Text = string.Empty;
            editZ.Text = string.Empty;
            editX.Text = string.Empty;
        }

        private void SCControl_Leave(object sender, EventArgs e) {
            try {
                CalcDZX();
            } catch {
            }
        }

        private void SCControl_EnabledChanged(object sender, EventArgs e) {
            label2.Visible = label3.Visible = label4.Visible = this.Enabled;
            label5.Visible = label6.Visible = label7.Visible = this.Enabled;
            edit0.Visible = edit1.Visible = edit3.Visible = this.Enabled;
            editD.Visible = editZ.Visible = editX.Visible = this.Enabled;
        }

        public void FocusOnEmptyEdit() {
            if (edit3.Text == string.Empty)
                edit3.Focus();
            else if (edit1.Text == string.Empty)
                edit1.Focus();
            else if (edit0.Text == string.Empty)
                edit0.Focus();
            else
                edit3.Focus();
        }
    }
}
